export default  class DesignUtils{
    constructor(props) {
        // 默认配置
    const {configJson} = props;
    const vitalsign = configJson.vitalsign || [];
    this.config = vitalsign[0] || {};   // 体温单配置，基本定义、底板定义、数据显示规律定义

    this.dis = props.scale || 1;    // 体温单的缩放比例, 默认为1
    this.unit = props.unit || 'mm';  // 体温单坐标显示单位，默认为mm(毫米)
    this.width = this.config['vitalsize'][0]['_attr'].width; // 体温单的宽度
    this.height = this.config['vitalsize'][0]['_attr'].height; // 体温单的高度
    this.printPadding = this.config['vitalsize'][0]['_attr'].printPadding; // 体温单打印上边距
    this.noSplit = this.config['vitalsize'][0]['_attr'].noSplit; // 是否一个格子拆分成4个小格
    this.printZoom = this.config['vitalsize'][0]['_attr'].printZoom || 1; // 体温单打印缩放比列
    this.printErrorValue = this.config['vitalsize'][0]['_attr'].printErrorValue || 0; // 误差值
    // 配置底版时，
    // left 对应距离左边距离为0
    // right 对应距离左边距离为体温单宽度
    // top 对应距离上边距离为0
    // bottom 对应距离上边宽度为体温单的高度
    // 用指代代替可便于改变宽高
    this.keyToNumber = {
      left: 0,
      right: this.width,
      top: 0,
      bottom: this.height
    };
      }


  /**
   * @method 设置svg显示宽高
   */
  getShowSize() {
    // 对外展示的体温单绘制数据部分的宽高
    const showWidth = this.width * this.dis + this.unit;
    const showHeight = this.height * this.dis + this.unit;

    return {
      showWidth: showWidth,
      showHeight: showHeight,
      width: this.width,
      height: this.height,
      printPadding: this.printPadding,
      noSplit: this.noSplit,
      printZoom: this.printZoom,
      headerPadding: this.headerPadding,
      printErrorValue: this.printErrorValue,
    }
  }

  /**
   * @method 获取被点击的矩形区域
   */
  getRectSize() {
    if (this.config['vitalreact']) {
      // 矩形区域的宽高， 被点击区域
      let {left, top, width, height} = this.config['vitalreact'][0]['_attr'];
      return {
        left: left * this.dis + this.unit,
        top: top * this.dis + this.unit,
        width: width * this.dis + this.unit,
        height: height * this.dis + this.unit,
        widthWithNoUnit: width,
        heightWithNoUnit: height,
      }
    } else {
      return null
    }
  }

  /**
   * @method 设置特殊字符i表示的数字
   * @returns {{left: number, right: *, top: number, bottom: *}|*}
   */
  getKeyToNumber() {
    return this.keyToNumber
  }


  /**
   * @method 获取字体id对应的样式
   * @param {Number} fontId
   * @returns {*|{}}
   */
  getFontIdToStyle(fontId) {
    let vitalFont = this.config['vitalfonts'][0]['vitalfont'];
    let fontIdToStyle = {};
    for (let i = 0; i < vitalFont.length; i++) {
      let attr = vitalFont[i]['_attr'];
      let {id} = attr;
      fontIdToStyle[id] = attr;
    }
    return fontId?fontIdToStyle[fontId]||{}:fontIdToStyle;
  }

  /**
   * @method 获取线条id对应的样式
   * @param lineId
   * @returns {*|{}}
   */
  getLineIdToStyle(lineId) {
    let vitalPen = this.config['vitalpens'][0]['vitalpen'];
    let lineIdToStyle = {};
    for (let i = 0; i < vitalPen.length; i++) {
      let attr = vitalPen[i]['_attr'];
      let {id} = attr;
      lineIdToStyle[id] = attr;
    }
    return lineIdToStyle[lineId] || lineIdToStyle;
  }

  /**
   * @method 获取符号id对应的样式
   * @param symbolId
   * @returns {*|{}}
   */
  getSymbolIdToStyle(symbolId) {
    let symbol = this.config['vitalsymbols'][0]['vitalsymbol'];
    let symbolIdToStyle = {};
    for (let i = 0; i < symbol.length; i++) {
      let attr = symbol[i]['_attr'];
      let {id, symbol1, symbol2, symbol3, symbol4, color, color1, color2, color3, color4, dxs, dys} = attr;
      //增加symbol4这个配置，symbol4是把符号写到symbol1符号的上面，symbol2把符号写到symbol1里面，symbol3把符号写到symbol1的右边
      let fontName = attr['font-name'];
      let fontSize = attr['font-size'];
      let fontWeight = attr['font-bold'] ? '600' : '500';
      let fontSizeArray = (fontSize? fontSize+'' : '').split(',');
      color = color ? `rgba(${color})` : 'rgba(0,0,0,1)';
      color1 = color1 ? `rgba(${color1})` : 'rgba(0,0,0,1)';
      color2 = color2 ? `rgba(${color2})` : 'rgba(0,0,0,1)';
      color3 = color3 ? `rgba(${color3})` : 'rgba(0,0,0,1)';
      color4 = color4 ? `rgba(${color4})` : 'rgba(0,0,0,1)';
      symbolIdToStyle[id] = {
        attr,
        symbol1, symbol2, symbol3, symbol4,
        color, color1, color2, color3, color4,
        fontName, fontWeight,
        fontSize: fontSizeArray[0],
        fontSize1: fontSizeArray[0],
        fontSize2: fontSizeArray[1],
        fontSize3: fontSizeArray[2],
        fontSize4: fontSizeArray[3],
        dxs: (dxs? dxs+'' : '').split(','),
        dys: (dys? dys+'' : '').split(','),
      };
    }
    return symbolIdToStyle[symbolId] || symbolIdToStyle;
  }

  /**
   * @method 获取底板线条数组
   * @returns {Array}
   */
  getVitalLines() {
    let vitalLines = this.config['vitallines'];
    let vitalLine = [];
    if (vitalLines && vitalLines[0]) {
      vitalLine = vitalLines[0]['vitalline'] || [];
    }
    return vitalLine
  }

  /**
   * @method 获取绘制底板文字
   * @returns {Array}
   */
  getVitalTexts() {
    let vitalTexts = this.config['vitaltexts'];
    let vitalText = [];
    if (vitalTexts && vitalTexts[0]) {
      vitalText = vitalTexts[0]['vitaltext'] || [];
    }
    return vitalText;
  }

  /**
   * @method 获取绘制连线数据，体温，脉搏等
   * @returns {Array}
   */
  getVitalLineOuts() {
    let vitalLineOut = [];
    if (this.config['vitallinedatas'] && this.config['vitallinedatas'][0]) {
      vitalLineOut = this.config['vitallinedatas'][0]['vitallineout'] || [];
      let vitalLineOuts = this.config['vitallinedatas'][0]['vitallineouts'];
      if (vitalLineOuts) {
        vitalLineOut = vitalLineOut.concat(vitalLineOuts[0]['vitallineout'] || [])
      }
    }
    return vitalLineOut;
  }

  /**
   * 获取体温脉搏心率之间的重叠规则
   */
  getVitalLineOverlaps(){
    let vitalLineOverlaps = [];
    if (this.config['vitallinedatas'] && this.config['vitallinedatas'][0]) {
      vitalLineOverlaps = this.config['vitallinedatas'][0]['vitallineoverlap'];
    }
    return vitalLineOverlaps;
  }

  /**
   * @method 获取体温单数据name对应的线上降温、升高线上体温、疼痛缓解、处理后的疼痛强度、物理降温 等特殊线条
   * @returns {*|Array}
   */
  getVitalLineBlocks() {
    let vitalLineBlock = [];
    if (this.config['vitallinedatas'] && this.config['vitallinedatas'][0]) {
      vitalLineBlock = this.config['vitallinedatas'][0]['vitallineblock'] || [];
      let vitalLineBlocks = this.config['vitallinedatas'][0]['vitallineblocks'];
      if (vitalLineBlocks) {
        vitalLineBlock = vitalLineBlock.concat(vitalLineBlocks[0]['vitallineblock'] || []);
      }
    }
    return vitalLineBlock
  }

  /**
   * @method 获取房颤等封闭多边形配置信息
   * @returns {*|Array}
   */
  getVitalRegionFills() {
    let vitalRegionFill = [];
    if (this.config['vitallinedatas'] && this.config['vitallinedatas'][0]) {
      vitalRegionFill = this.config['vitallinedatas'][0]['vitalregionfill'] || [];
      let vitalRegionFills = this.config['vitallinedatas'][0]['vitalregionfills'];
      if (vitalRegionFills) {
        vitalRegionFill = vitalRegionFill.concat(vitalRegionFills[0]['vitalregionfill'] || [])
      }
    }

    return vitalRegionFill;
  }

  /**
   * @method 获取数据文字配置属性
   * @return {Object}
   */
  getVitalTextOuts() {
    let vitalTextOuts = this.config['vitaltextdatas'];
    let vitalTextOut = [];
    if (vitalTextOuts && vitalTextOuts[0]) {
      vitalTextOut = vitalTextOuts[0]['vitaltextout'];
    }
    return vitalTextOut;
  }

  /**
   * 获取体温单数据name的配置信息
   */
  getVitalShows(){
    let vitalShows = this.config['vitalshows'];
    let vitalShow = [];
    if (vitalShows && vitalShows[0]) {
      vitalShow = vitalShows[0]['vitalshow']||[];
    }
    return vitalShow;
  }

  /**
   * @method 获取绘制折线(脉搏、心率、体温等)配置信息
   * @param {String} name
   * @return {Object}
   */
  getVitalLineOutToConfig(name) {
    let vitalLineOut = this.getVitalLineOuts();

    console.log("111getVitalLineOutToConfig",vitalLineOut);

    let vitalLineOutToConfig = {};
    for (let i = 0; i < vitalLineOut.length; i++) {
      let attr = vitalLineOut[i]['_attr'];
      let {name} = attr;
      vitalLineOutToConfig[name] = attr;
    }

    return name?vitalLineOutToConfig[name]||{} : vitalLineOutToConfig;
  }

  /**
   * @method 获取绘制折线(脉搏、心率、体温等)配置信息
   * @param {String} name
   * @return {Object}
   */
  getVitalLineOverlapToConfig(name) {
    let vitalLineOverlaps = this.getVitalLineOverlaps()||[];
    let vitalLineOverlapToConfig = {};
    for (let i = 0; i < vitalLineOverlaps.length; i++) {
      let attr = vitalLineOverlaps[i]['_attr'];
      let {name} = attr;
      vitalLineOverlapToConfig[name] = attr;
    }

    return name?vitalLineOverlapToConfig[name]||{}: vitalLineOverlapToConfig;
  }

  /**
   * @method 获取体温单数据name对应的线上降温、升高线上体温、疼痛缓解、处理后的疼痛强度、物理降温 等特殊线条配置信息
   * @param {String} name
   * @return {Object}
   */
  getVitalLineBlockToConfig(name) {
    let vitalLineBlock = this.getVitalLineBlocks();

    let vitalLineBlockToConfig = {};
    for (let i = 0; i < vitalLineBlock.length; i++) {
      let attr = vitalLineBlock[i]['_attr'];
      let {name} = attr;
      vitalLineBlockToConfig[name] = attr;
    }

    return name?vitalLineBlockToConfig[name]||{}:vitalLineBlockToConfig;
  }


  /**
   * @method 获取体温单填充线配置信息
   * @param {String|null} name
   * @return {Object}
   */
  getVitalRegionFillToConfig(name) {
    let vitalLineFill = this.getVitalRegionFills();
    let vitalRegionFillToConfig = {};

    for (let i = 0; i < vitalLineFill.length; i++) {
      let attr = vitalLineFill[i]['_attr'];
      let {name} = attr;
      vitalRegionFillToConfig[name] = attr;
    }

    return name?vitalRegionFillToConfig[name]||{}: vitalRegionFillToConfig;
  }

  /**
   * @method 获取体温单数据name对应的配置信息
   * @param {String} name
   * @return {Object}
   */
  getVitalTextOutToConfig(name) {
    let vitalTextOut = this.getVitalTextOuts();
    let nameToConfig = {};

    for (let i = 0; i < vitalTextOut.length; i++) {
      let attr = vitalTextOut[i]['_attr'];
      let {x, y, cx, cy, name, align, split, ...other} = attr;
      x = x.toString().replace(/([^0-9]*)/, (k) => {
        return this.keyToNumber[k] === undefined ? k : this.keyToNumber[k]
      }) - 0;

      y = y.toString().replace(/([^0-9]*)/, (k) => {
        return this.keyToNumber[k] === undefined ? k : this.keyToNumber[k]
      }) - 0;

      cx = cx.toString().replace(/([^0-9]*)/, (k) => {
        return this.keyToNumber[k] === undefined ? k : this.keyToNumber[k]
      }) - 0;

      cy = cy.toString().replace(/([^0-9]*)/, (k) => {
        return this.keyToNumber[k] === undefined ? k : this.keyToNumber[k]
      }) - 0;
      let fontId = attr['font-id'] || 1;
      let fontStyle = this.getFontIdToStyle(fontId);
      let style = {
        fontSize: fontStyle['font-size'],
        fontFamily: fontStyle['font-name'],
        fontWeight: fontStyle['font-bold'] ? '600' : '500',
        textDecoration: fontStyle['font-underline'] ? 'underline' : '',
        textAnchor: align === 'center' || align === 'vertical' ? 'middle' : 'middle',
        dominantBaseline: 'central',
        fontInital: fontStyle['font-inital'] ? 'italic' : '', // 斜体,
        color: fontStyle['text-color'] ? `rgba(${fontStyle['text-color']})` : ''
      };
      nameToConfig[name] = {
        ...other,
        x: x,
        y: y,
        cx: cx,
        cy: cy,
        name: name,
        align: align,
        style: style,
        split: split,
      }
    }

    // 添加UP/DOWN 偏移
    for(let name in nameToConfig){
      if(name.indexOf('UP')===-1 && name.indexOf('DOWN') ===-1){
        const upName = `${name}UP`;
        const downName = `${name}DOWN`;
        const offset = (nameToConfig[name].offset||1) - 0;
        if(!nameToConfig[upName]){
          nameToConfig[upName] = {
            ...nameToConfig[name],
            y: nameToConfig[name].y - offset,
          }
        }
        if(!nameToConfig[downName]){
          nameToConfig[downName] = {
            ...nameToConfig[name],
            y: nameToConfig[name].y + offset,
          }
        }
      }
    }
    return nameToConfig;
  }

  /**
   * 获取体温单数据name的配置信息（体温单配置->显示配置）
   * 配置信息目前有
   * lineCount 每行显示几个
   * spaceMark 分隔符
   */
  getVitalShowToConfig(){
    let vitalShows = this.getVitalShows();
    let nameToConfig = {};
    for (let i = 0; i < vitalShows.length; i++) {
      let attr = vitalShows[i]['_attr'];
      nameToConfig[attr.name] = attr
    }
    return nameToConfig;
  }



}